clear;
close all;
clc;

%============Change the following parameters
Proto_Num = 31
degree = 30
for I_trial = 1:10 % change I_trial from 1 to 10, which will correspond to the values below.


    precuts  =10+[ 3.7 3.9 1.9 1.7 2.1 1.5 1.8 1.5 1.3 1.4 ];% ********* Protocol 3.2 this is good for 3.1


    %============Do not change the following parameters
    fs = 44100;

    [b a]=butter(3,1/fs,'high');
    precut=precuts(I_trial)
    [YY,fs2]=audioread(['3Dio_all\Protocol_' num2str(Proto_Num) '\Protocol_' num2str(Proto_Num) '_deg' num2str(degree) '_' num2str(I_trial) '.mp3']);
    recorded_sound=resample(YY,441,320);

    load 'C:\Users\gaiy\OneDrive - Saint Louis University\speech_experiments\ori';

    % % % % % % % [Y Ind]=findpeaks(sound_left,32000,'MinPeakHeight',0.15)
    % % % % % % % true_dur = diff(Ind)
    % % % % % % % save record_durs true_dur
    load record_durs
    % dur = [true_dur true_dur(end)]+0.14
    dur = [true_dur true_dur(end)]

    % sound([sound_left' 0.*sound_left'],Fs,16)
    % recorded_sound=resample(sound_left.*10,fs,Fs);
    % sound([recorded_sound' 0.*recorded_sound'],fs,16)


    left_sound = filter(b,a,recorded_sound(:,1));
    right_sound = filter(b,a,recorded_sound(:,2));
    % left_sound = filter(b,a,recorded_sound');
    % right_sound = filter(b,a,recorded_sound');

    % sound(left_sound,fs);
    % pause

    left_sound=left_sound(round(precut.*fs):end);
    right_sound=right_sound(round(precut.*fs):end);

    figure;
    plot(left_sound,'b')
    hold on;
    plot(right_sound,'r')
    % sdf9
    % sound(recorded_sound,fs);
    % pause(length(recorded_sound)./fs.*0.8)

    % cutting:
    for ip = 1:24
        ip
        time_sofar= sum(dur(1:ip-1));
        durN = round(dur(ip).*fs);
        startP=round(time_sofar*fs);
        pcut = [left_sound(startP+1:min([end startP+durN])) right_sound(startP+1:min([end startP+durN]))];
        wholeramp=f_getRamp(0.05,length(pcut)./fs,fs);
        pcut =pcut .*wholeramp';
        area([startP+1 startP+durN],[0.4 0.4],0.2)
        title(num2str(ip))

%         sound(pcut,fs);
%         pause(dur(ip)+1)

        eval(['save 3Dio_all\cut_P' num2str(Proto_Num) '\Phoneme_3Diocontrol' num2str(ip) '_v' num2str(I_trial) ' pcut fs']);

        % pause9
    end

end